﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.Core.Snowflake;
using ZilLion.Service.WeChatMall.Domain.Entitys.Cust;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys
{
    public class CustAddressRepository : SqlServerRespository<CustAddressEntity>,
        ICustAddressRepository
    {
        public CustAddressRepository()
        {
            Context = new WechatMallContext();
        }

        public IEnumerable<CustAddressEntity> GetAddressList(long custid)
        {
            const string sql = @"SELECT  custid AS Custid ,
                                dzid AS AddressId ,
                                sex AS Sex ,
                                jdwd AS Location ,
                                cscode AS Adcode ,                                
                                [address] AS GpsAddress ,
                                shaddress AS DetailAddress ,
                                defaultaddr AS IsDefault ,
                                shr AS Receiver ,
                                shrphonenum AS ReceiverTel
                                FROM    weisccustjfaddr where custid=@custid ";
            return GetList(sql, new {custid});
        }

        public CustAddressEntity SaveAddress(CustAddressEntity entity)
        {
            if (entity.AddressId == 0)
            {
                var id = SnowflakeIdCreater.Instance().GetNextId();
                var list = GetAddressList(entity.Custid).ToList();
                if (!list.Any())
                    entity.IsDefault = 1; // 如果没有地址,则新加的第一个地址同时需要设置成默认地址
                entity.AddressId = id;
                Add(entity);
            }
            else
            {
                Modify(entity);
            }
            return entity;
        }

        public CustAddressEntity GetDefaultAddress(string adcode, long custid)
        {
            var list = GetAddressList(custid).ToList();
            var defaultEntity = list.FirstOrDefault(x => x.IsDefault == 1);
            if (defaultEntity != null)
                return defaultEntity;
            var first = list.FirstOrDefault(x => x.Adcode == adcode);
            return first;
        }

        public CustAddressEntity SetDefaultAddress(long addressid)
        {
            var entity = GetAddress(addressid);
            if (entity == null) return null;
            entity.IsDefault = 1;
            const string sql = @"SELECT  custid AS Custid ,
                                dzid AS AddressId ,
                                sex AS Sex ,
                                jdwd AS Location ,
                                cscode AS Adcode ,                                
                                [address] AS GpsAddress ,
                                shaddress AS DetailAddress ,
                                defaultaddr AS IsDefault ,
                                shr AS Receiver ,
                                shrphonenum AS ReceiverTel
                                FROM    weisccustjfaddr where custid=@custid and defaultaddr=1";


            var list = GetList(sql, new {custid = entity.Custid});

            using (var unit = new UnitOfWork())
            {
                foreach (var defalutaddress in list)
                {
                    defalutaddress.IsDefault = 0;
                    Modify(defalutaddress, unit);
                }
                Modify(entity, unit);
                unit.Complete();
            }
            return entity;
        }

        public CustAddressEntity GetAddress(long addressid)
        {
            const string sql = @"SELECT  custid AS Custid ,
                                dzid AS AddressId ,
                                sex AS Sex ,
                                jdwd AS Location ,
                                cscode AS Adcode ,                                
                                [address] AS GpsAddress ,
                                shaddress AS DetailAddress ,
                                defaultaddr AS IsDefault ,
                                shr AS Receiver ,
                                shrphonenum AS ReceiverTel
                                FROM    weisccustjfaddr where dzid=@addressid ";
            var entity = GetList(sql, new {addressid}).FirstOrDefault();

            return entity;
        }

        public void RemoveAddress(CustAddressEntity entity)
        {
            Remove(entity);
        }
    }
}